<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="供应商名称" prop="supplierName">
      <el-input v-model="dataForm.supplierName" placeholder="供应商名称"></el-input>
    </el-form-item>
    <el-form-item label="联系人名称" prop="contactName">
      <el-input v-model="dataForm.contactName" placeholder="联系人名称"></el-input>
    </el-form-item>
    <el-form-item label="联系人手机" prop="contactMobile">
      <el-input v-model="dataForm.contactMobile" placeholder="联系人手机"></el-input>
    </el-form-item>
    <el-form-item label="联系人电话" prop="contactTelephone">
      <el-input v-model="dataForm.contactTelephone" placeholder="联系人电话"></el-input>
    </el-form-item>
    <el-form-item label="银行账号" prop="bankAccount">
      <el-input v-model="dataForm.bankAccount" placeholder="银行账号"></el-input>
    </el-form-item>
    <el-form-item label="供应商地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="供应商地址"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {SupplierService} from "../../../model/gs/SupplierService";
  const supplierService = SupplierService.getInstance()

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          sid: 0,
          supplierName: '',
          contactName: '',
          contactMobile: '',
          contactTelephone: '',
          bankAccount: '',
          address: '',
          hospitalId: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          supplierName: [
            { required: true, message: '供应商名称不能为空', trigger: 'blur' }
          ],
          contactName: [
            { required: true, message: '联系人名称不能为空', trigger: 'blur' }
          ],
          contactMobile: [
            { required: true, message: '联系人手机不能为空', trigger: 'blur' }
          ],
          contactTelephone: [
            { required: true, message: '联系人电话不能为空', trigger: 'blur' }
          ],
          bankAccount: [
            { required: true, message: '银行账号不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '供应商地址不能为空', trigger: 'blur' }
          ],
          hospitalId: [
            { required: true, message: '医院id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.sid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.sid) {
            supplierService.queryById(id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.supplierName = data.data.supplierName
                this.dataForm.contactName = data.data.contactName
                this.dataForm.contactMobile = data.data.contactMobile
                this.dataForm.contactTelephone = data.data.contactTelephone
                this.dataForm.bankAccount = data.data.bankAccount
                this.dataForm.address = data.data.address
                this.dataForm.hospitalId = data.data.hospitalId
                this.dataForm.createTime = data.data.createTime
                this.dataForm.updateTime = data.data.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            supplierService.saveOrUpdate(this.dataForm).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.message)
              }
            })
          }
        })
      }
    }
  }
</script>
